{% extends "admin_base.html" %}

{% block title %}Viewing Server{% endblock %}

{% block content %}
<div class="col-9">
	<ul class="nav nav-tabs" id="config_tabs">
		<li class="active"><a href="#info" data-toggle="tab">Connection</a></li>
		<li><a href="#server_sett" data-toggle="tab">Settings</a></li>
		<li><a href="#ftp_sett" data-toggle="tab">FTP</a></li>
		<li><a href="#delete" data-toggle="tab">Delete</a></li>
		<li><a href="../../servers.php?goto={{ server.hash }}">Server Control</a></li>
	</ul>
	{% if get.disp is defined and get.disp != 1 %}
		<br />
		<div class="alert alert-danger">
			{% if get.disp == 'n_fail' %}
				The server name does not meet the requirements (4-35 characters, a-z A-Z 0-9 _ -).
			{% elseif get.disp == 'o_fail' %}
				The node daemon must be online before updating servers!
			{% endif %}
		</div>
	{% endif %}
	<div class="tab-content">
		<div class="tab-pane active" id="info">
			<h3>Connection Information</h3><hr />
			<form action="ajax/server/connection.php" method="POST">
				<fieldset>
					<div class="panel panel-default">
						<div class="panel-heading">Changing IP &amp; Port</div>
						<div class="panel-body">
							<p>If you want to change the Server IP then select an IP from the list below that has at least one available port. When you select a new IP you will be prompted to select a new port from a list. If you only want to change the port, and not the IP, then you can do so by simply selecting an available port.</p>
							<p>Current Connection Address: <code>{{ server.server_ip }}:{{ server.server_port }}</code></p>
						</div>
					</div>
					<div class="form-group col-6 nopad">
						<label for="server_ip" class="control-label">Server IP</label>
						<div>
							<select name="server_ip" id="server_ip" class="form-control">
								{% for ip, internal in decoded.ips %}
									<option
										{% if internal.ports_free > 0 %}
											value="{{ ip }}"
											{% if server.server_ip == ip %}
												selected="selected"
											{% endif %}
										{% else %}
											disabled="disabled"
										{% endif %}
									>{{ ip }} ({{ internal.ports_free }} Avaliable Ports)</option>
								{% endfor %}
							</select>
						</div>
					</div>
					<div class="form-group col-6 nopad-right">
						<label for="server_ip" class="control-label">Server Port</label>
						<div>
							{% for ip, internal in decoded.ports %}
							<span {% if server.server_ip != ip %}style="display:none;"{% endif %} id="node_{{ ip }}">
								<select name="server_port_{{ ip }}" class="form-control">';
								{% for port, avaliable in internal %}
									<option
										{% if server.server_port == port %}selected="selected"{% endif %}
										{% if avaliable == 1 %}value="{{ port }}"{% else %}disabled="disabled"{% endif %}
									>{{ port }}</option>
								{% endfor %}
								</select>
							</span>
							{% endfor %}
						</div>
					</div>
					<input type="hidden" name="sid" value="{{ get.id }}" />
					<input type="hidden" name="nid" value="{{ node.id }}" />
					<input type="submit" value="Update Server" class="btn btn-primary btn-sm" />
				</fieldset>
			</form>
		</div>
		<div class="tab-pane" id="server_sett">
			<h3>Server Settings</h3><hr />
			<form action="ajax/server/allocate.php" method="post">
				<fieldset>
					<div class="form-group">
						<label class="control-label">Server Name</label>
						<div>
							<input type="text" name="server_name" value="{{ server.name }}" class="form-control" />
						</div>
					</div>
					<div class="form-group">
						<label class="control-label">Owner Email</label>
						<div>
							<input type="text" readonly="readonly" value="{{ user.username }} ({{ user.email }})" class="form-control" />
						</div>
					</div>
					<div class="form-group">
						<label class="control-label">GSD Token</label>
						<div>
							<input type="text" readonly="readonly" value="{{ server.gsd_secret }}" class="form-control" />
						</div>
					</div>
					<div class="row">
						<div class="form-group col-6 nopad">
							<label for="alloc_mem" class="control-label">Allocate RAM</label>
							<div class="input-group">
								<input type="text" autocomplete="off" name="alloc_mem" value="{{ server.max_ram }}" class="form-control" />
								<span class="input-group-addon">MB</span>
							</div>
						</div>
						<div class="form-group col-6 nopad-right">
							<label for="alloc_disk" class="control-label">Disk Space</label>
							<div class="input-group">
								<input type="text" name="alloc_disk" value="{{ server.disk_space }}" class="form-control" />
								<span class="input-group-addon">MB</span>
							</div>
						</div>
					</div>
					<input type="hidden" name="sid" value="{{ get.id }}" />
					<input type="submit" value="Update Server Settings" class="btn btn-primary btn-sm" />
				</fieldset>
			</form>
		</div>
		<div class="tab-pane" id="ftp_sett">
			<h3>FTP Settings</h3><hr />
			<form action="ajax/server/ftp.php" method="post">
				<fieldset>
					<div class="form-group">
						<label for="ftp_host" class="control-label">Host</label>
						<div>
							<input type="text" readonly="readonly" value="{{ node.fqdn }} ({{ node.ip }})" class="form-control" />
						</div>
					</div>
					<div class="form-group">
						<label for="ftp_user" class="control-label">Username</label>
						<div>
							<input type="text" readonly="readonly" value="{{ server.ftp_user }}-{{ server.gsd_id }}" class="form-control" />
						</div>
					</div>
					<div class="well">
						<div class="row">
							<div class="alert alert-success" style="display:none;margin-bottom:10px;" id="gen_pass"></div>
							<div class="form-group col-6 nopad">
								<label for="ftp_pass" class="control-label">New Password</label>
								<div class="input-group">
									<input type="password" autocomplete="off" name="ftp_pass" class="form-control" />
									<span class="input-group-btn">
										<button class="btn btn-success" id="gen_pass_bttn" type="button">Generate</button>
									</span>
								</div>
							</div>
							<div class="form-group col-6 nopad-right">
								<label for="ftp_pass_2" class="control-label">New Password (Again)</label>
								<div>
									<input type="password" autocomplete="off" name="ftp_pass_2" class="form-control" />
								</div>
								<div class="checkbox" style="margin-bottom:-20px;">
									<label>
										<input type="checkbox" name="email_user" /> Email new password to user.
									</label>
								</div>
							</div>
						</div>
					</div>
					<input type="hidden" name="sid" value="{{ get.id }}" />
					<input type="hidden" name="nid" value="{{ node.id }}" />
					<input type="submit" value="Update Password" class="btn btn-primary btn-sm" />
				</fieldset>
			</form>
		</div>
		<div class="tab-pane" id="delete">
			<h3>Delete Server</h3><hr />
				<div class="alert alert-danger"><strong><i class="fa fa-warning"></i></strong> Deleting a server is an irreversiable action. Proceede with caution. This server will be removed from the database along with all files on the server.</div>
				<form action="ajax/server/delete.php" method="POST">
					<input type="hidden" name="sid" value="{{ get.id }}" />
					<input type="submit" name="delete" class="btn btn-sm btn-danger" value="Permanetly Delete this Server" />
				</form>
		</div>
	</div>
</div>
{% endblock %}

{% block javascript %}
<script type="text/javascript">
	$(document).ready(function(){
		setActiveOption('server-find');
		if($.urlParam('error') != null){
			var field = $.urlParam('error');
			var exploded = field.split('|');
				$.each(exploded, function(key, value) {
					$('[name="' + value + '"]').parent().parent().addClass('has-error');
				});
		}

		$("#server_ip").change(function() {
			var ip = $(this).val().replace(/\./g, "\\.");
			$("[id^=node_]").hide();
			$("#node_"+ip).show();
		});
		$("#gen_pass_bttn").click(function(){
			$.ajax({
				type: "GET",
				url: "add.php?do=generate_password",
				success: function(data) {
					$("#gen_pass").html('<strong>Generated Password:</strong> '+data);
					$("#gen_pass").slideDown();
					$('input[name="ftp_pass"]').val(data);
					$('input[name="ftp_pass_2"]').val(data);
					return false;
				}
			});
			return false;
		});
	});
</script>
{% endblock %}
